package wechat.handler.bean.response.message.reply;

import java.util.List;

import org.dom4j.DocumentException;
import org.dom4j.Element;

import wechat.handler.bean.response.MessageBaseBean;


/**
 * 图文消息
 * 
 *
 */
public class ReplyImageTextBean extends MessageBaseBean {

	private List<ReplyImageTextItemBean> Articles; //多条图文消息信息，默认第一个item为大图,注意，如果图文数超过10，则将会无响应
	private int ArticleCount;  // 图文消息个数，限制为10条以内
	
	public ReplyImageTextBean(String str) throws DocumentException {
		super(str);
		super.setMsgType(Reply_Message_News);
	}
	
	public ReplyImageTextBean(){
		super.setMsgType(Reply_Message_News);
	}

	
	public int getArticleCount() {
		return ArticleCount;
	}

	private void setArticleCount(int articleCount) {
		ArticleCount = articleCount;
	}
	
	public List<ReplyImageTextItemBean> getArticles() {
		return Articles;
	}

	public void setArticles(List<ReplyImageTextItemBean> articles) {
		Articles = articles;
	}
	
	
//	protected static final String Xpath_PicUrl = "/xml/PicUrl";
//	protected static final String Xpath_MediaId = "/xml/MediaId";
//	
//	public void transform() throws Exception {
//		super.transform();
//		this.setPicUrl(getNodeText(Xpath_PicUrl));
//		this.setMediaId(getNodeText(Xpath_MediaId));
//	}
	
	
	public String buildXml() throws Exception {
		super.buildXml();
		
		List<ReplyImageTextItemBean> replyImageTextItemBeans = getArticles();
		int articleCount = replyImageTextItemBeans.size();
		if (articleCount > 10) {
			throw new Exception("图文消息个数，限制为10条以内");
		}
		setArticleCount(articleCount);
		
		super.addNodeValue("ArticleCount", String.valueOf(getArticleCount()));
		Element Articles = super.addNode("Articles");
		for (int i = 0; i < articleCount; i++) {
			ReplyImageTextItemBean replyImageTextItemBean = replyImageTextItemBeans.get(i);
			Element item = Articles.addElement("item");
			item.addElement("Title").addCDATA(replyImageTextItemBean.getTitle());
			item.addElement("Description").addCDATA(replyImageTextItemBean.getDescription());
			item.addElement("PicUrl").addCDATA(replyImageTextItemBean.getPicUrl());
			item.addElement("Url").addCDATA(replyImageTextItemBean.getUrl());

		}
		
		return parent.asXML();
	}



}
